// 手机号码脱敏
export const phoneMask = (v = '') => v?.replace(/^(\d{3})\d*(\d{4})$/, '$1****$2');

/**
 * 处理音频时间格式转换：秒转换为mm:ss
 * @param sec 当前需转换的时间
 * @returns 转化后时间
 */
export const convertTimeSecToMmSs = (sec: number) => {
	const s = sec % 60 < 10 ? `0${sec % 60}` : sec % 60;
	const min = Math.floor(sec / 60) < 10 ? `0${Math.floor(sec / 60)}` : Math.floor(sec / 60);
	return `${min}:${s}`;
};
// 时间转换
export const secondsToTime = (secs: number) => {
	const hours = Math.floor(secs / (60 * 60));
	const divisorminutes = secs % (60 * 60);
	const minutes = Math.floor(divisorminutes / 60);
	const divisorseconds = divisorminutes % 60;
	const seconds = Math.ceil(divisorseconds);

	const obj = {
		h: hours,
		m: minutes,
		s: seconds
	};
	return obj;
};
// 获取父级region
export const getCompleteRegion = (region: string): any => {
	// 省级地区编码后缀
	const provinceCodeSuffix = '0000';
	// 市级地区编码后缀
	const cityCodeSuffix = '00';
	// 乡镇地区编码后缀
	const townCodeSuffix = '000';
	// 常规省市区地区编码
	if (region && region.length === 6) {
		if (region.endsWith(provinceCodeSuffix)) {
			return region;
		} else if (region.endsWith(cityCodeSuffix)) {
			return `${region.substring(0, 2) + provinceCodeSuffix},${region}`;
		} else {
			return `${region.substring(0, 2) + provinceCodeSuffix},${region.substring(0, 4)}${cityCodeSuffix},${region}`;
		}
	} else if (region && region.length === 12) {
		// 乡镇地区编码处理
		if (region.endsWith(townCodeSuffix)) {
			return `${getCompleteRegion(region.substring(0, 6))},${region}`;
		} else {
			return `${getCompleteRegion(region.substring(0, 6))},${region.substring(0, 9)}${townCodeSuffix},${region}`;
		}
	}
	return region;
};

// 提取纯文本
// eslint-disable-next-line consistent-return
export const getSimpleText = (html: any) => {
	if (html) {
		const re1 = new RegExp('<.+?>', 'g');
		const msg = html.replace(re1, '');
		return msg;
	}
};
